package com.example.bzh.gamecat.ui.message.search;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.bzh.gamecat.R;
import com.example.bzh.gamecat.base.BaseActivity;

import butterknife.BindView;
import butterknife.OnClick;

public class SearchActivity extends BaseActivity {

    @BindView(R.id.et_search)
    EditText etSearch;
    @BindView(R.id.tv_search_cancle)
    TextView tvSearchCancle;
    @BindView(R.id.iv_search_clear)
    ImageView ivSearchClear;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_search;
    }

    @Override
    public void initView() {
        super.initView();
        //搜索框的输入监听事件
        etSearch.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int start, int after, int count) {
                if (etSearch.getText().toString().length() != 0) {
                    tvSearchCancle.setText("搜索");
                    ivSearchClear.setVisibility(View.VISIBLE);
                } else {
                    tvSearchCancle.setText("取消");
                    ivSearchClear.setVisibility(View.INVISIBLE);
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
    }

    @OnClick({R.id.iv_search_clear, R.id.tv_search_cancle})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_search_clear:
                etSearch.setText("");
                ivSearchClear.setVisibility(View.GONE);
                break;
            case R.id.tv_search_cancle:
                if (tvSearchCancle.getText().toString().equals("取消")) {
                    finish();
                } else {
                    // TODO: 2016/8/13 进行搜索(数据库查询)
                }
                break;
        }
    }
}
